//
//  SSVisitorView.swift
//  SSSina
//
//  Created by ss on 2017/6/29.
//  Copyright © 2017年 ss. All rights reserved.
//

import UIKit
import SnapKit

class SSVisitouView: UIView {
    
    // 01 声明闭包
    var closure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //监听登陆按钮点击事件
    @objc private func loginClick(){
        
        //03执行闭包
        closure?()
    }
    
    //提供一个供外界调用的方法(设置消息 发现 我的logo 和 文字 但是还得和首页区分,因为首页有旋转动画)
    //首页需要一个圆圈的旋转动画, 其他界面还要隐藏圆圈
    func setupVisitorViewInfo(imgName:String?,title:String?){
        //判断imgName 和title是否为nil 如果都为nil 就是首页
        //其他的就是消息,发现,我
        if let img = imgName,let tit = title {
            logoImageView.image = UIImage(named:img)
            messageLabel.text = tit
            //隐藏圆圈
            circleImageView.isHidden = true
        }else {
            //是首页的话 添加首页圆圈动画
            setupCircleImageViewAnim()
        }
    }
    //设置圆圈的旋转动画
    private func setupCircleImageViewAnim(){
        //实例化动画对象
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //动画时间
        anim.duration = 10
        //旋转的角度
        anim.toValue = Double.pi * 2
        //重复次数
        anim.repeatCount = MAXFLOAT
        
        //当切换控制器的时候,或者重新进入程序的时候,动画会停止运行,解决此bug不让它停止运行  isRemovedOnCompletion 设置成false 不停止动画
        anim.isRemovedOnCompletion = false
        //将动画添加到圆圈上
        circleImageView.layer.add(anim, forKey: nil)
        
    }
    
    //搭建界面
    private func setupUI(){
        
        // 设置视图
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        
        // 01 添加控件
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(logoImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        // 02 添加约束
        // 圆圈
        circleImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        // 遮挡
        maskImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        // logo
        logoImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        // 文字
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
          //make.width.equalTo(230)
            make.top.equalTo(maskImageView.snp.bottom).offset(16)
        }
        
        // 注册按钮
        registerButton.snp.makeConstraints { (make) in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp.bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        //登陆按钮
        loginButton.snp.makeConstraints { (make) in
            make.right.equalTo(messageLabel)
            make.size.top.equalTo(registerButton)
        }
        
        
    }
    
    // 懒加载控件
    // 圆圈
    private lazy var circleImageView: UIImageView = UIImageView(named: "visitordiscover_feed_image_smallicon")
    // 遮挡
    private lazy var maskImageView: UIImageView = UIImageView(named: "visitordiscover_feed_mask_smallicon")
    // logo
    private lazy var logoImageView: UIImageView = UIImageView(named: "visitordiscover_feed_image_house")
    // 文字
    private lazy var messageLabel: UILabel = UILabel(text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", textColor: UIColor.darkGray, fontSize: SSNormalFontSize, maxWidth: 230)
    
    // 注册按钮
    private lazy var registerButton: UIButton = UIButton(title: "注册", fontSize: SSNormalFontSize, normalColor: UIColor.darkGray, highlightedColor: SSThemeColor, backgroundImage: "common_button_white", target: self, action: #selector(loginClick))
    
    // 登录按钮
    private lazy var loginButton: UIButton =  UIButton(title: "登录", fontSize: 14, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange, backgroundImage: "common_button_white", target: self, action: #selector(loginClick))
    
    
}
